﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Sample.JwtBearer.Api.Requests;
using Sample.JwtBearer.Api.Responses;
using Sample.JwtBearer.Api.Services;

namespace Sample.JwtBearer.Api.Controllers;

/// <summary>
/// User
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }

    /// <summary>
    /// Register
    /// </summary>
    /// <param name="register"></param>
    /// <returns></returns>
    [HttpPost("Register")]
    [ProducesResponseType(typeof(TokenResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(FailedResponse), StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> RegisterAsync(RegisterRequest register, CancellationToken ct)
    {
        var result = await _userService.RegisterAsync(register);
        if (!result.Success)
        {
            return BadRequest(new FailedResponse()
            {
                Errors = result.Errors
            });
        }

        return Ok(new TokenResponse
        {
            AccessToken = result.AccessToken,
            TokenType = result.TokenType,
            ExpiresIn = result.ExpiresIn,
            RefreshToken = result.RefreshToken,
            ExpiryTimeOfDay = result.ExpiryTimeOfDay
        });
    }

    /// <summary>
    /// Login
    /// </summary>
    /// <param name="login"></param>
    /// <returns></returns>
    [HttpPost("Login")]
    [ProducesResponseType(typeof(TokenResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(FailedResponse), StatusCodes.Status401Unauthorized)]
    public async Task<IActionResult> LoginAsync(LoginRequest login, CancellationToken ct)
    {
        var result = await _userService.LoginAsync(login);
        if (!result.Success)
        {
            return Unauthorized(new FailedResponse()
            {
                Errors = result.Errors
            });
        }

        return Ok(new TokenResponse
        {
            AccessToken = result.AccessToken,
            TokenType = result.TokenType,
            ExpiresIn = result.ExpiresIn,
            RefreshToken = result.RefreshToken,
            ExpiryTimeOfDay = result.ExpiryTimeOfDay
        });
    }

    /// <summary>
    /// RefreshToken
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("RefreshToken")]
    [ProducesResponseType(typeof(TokenResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(FailedResponse), StatusCodes.Status401Unauthorized)]
    public async Task<IActionResult> RefreshTokenAsync(RefreshTokenRequest request, CancellationToken ct)
    {
        var result = await _userService.RefreshTokenAsync(request);
        if (!result.Success)
        {
            return Unauthorized(new FailedResponse()
            {
                Errors = result.Errors
            });
        }

        return Ok(new TokenResponse
        {
            AccessToken = result.AccessToken,
            TokenType = result.TokenType,
            ExpiresIn = result.ExpiresIn,
            RefreshToken = result.RefreshToken,
            ExpiryTimeOfDay = result.ExpiryTimeOfDay
        });
    }

}